package com.git.groovy.utils;

import com.git.groovy.service.ITestService;
import groovy.lang.GroovyClassLoader;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;

@Slf4j
public class GroovyUtil {

    public static <T> T loaderClassAndSpring(String filePath) throws IllegalAccessException, IOException, InstantiationException {
        T object = null;
        try {
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
        Class groovyClass  = groovyClassLoader.parseClass(new File(filePath));
        object = (T) groovyClass.newInstance();
        } catch (Exception e) {
            log.error("classLoader.parseClass", e);
            throw e;
        }
        SpringContextUtils.autowireBean(object);//可以实现自动注入bean，
        log.info("【{}】脚本加载结束" ,filePath);
        return object;
    }


    public static <T> T loaderClass(String filePath) throws IllegalAccessException, IOException, InstantiationException {
        boolean isSuccess = true;
        T object = null;
        try {
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
        Class groovyClass  = groovyClassLoader.parseClass(new File(filePath));
        object = (T) groovyClass.newInstance();
        } catch (Exception e) {
            log.error("classLoader.parseClass", e);
            isSuccess = false;
            throw e;
        }
        log.info("【{}】脚本加载结束" ,filePath);
        return object;
    }

    public static <T> T loaderClassForString(String str,String calssName) throws IllegalAccessException, IOException, InstantiationException {
        T object = null;
        try {
            GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
            Class groovyClass  = groovyClassLoader.parseClass(str,calssName);
            object = (T) groovyClass.newInstance();
        } catch (Exception e) {
            log.error("classLoader.parseClass", e);
            throw e;
        }
        log.info("【{}】脚本加载结束" ,calssName);
        return object;
    }

    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
        String str = "import com.git.groovy.service.HelloService\n" +
                "import com.git.groovy.service.ITestService\n" +
                "import org.springframework.beans.factory.annotation.Autowired\n" +
                "\n" +
                "public class TestGroovyService implements ITestService{\n" +
                "\n" +
                "\n" +
                "    @Autowired\n" +
                "    HelloService helloService;\n" +
                "\n" +
                "    @Override\n" +
                "    String sayHello(String str) {\n" +
                "        println str;\n" +
                "        return \"HI \" + str;\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    String sayHelloTestSpring(String str) {\n" +
                "        println str;\n" +
                "        String res = helloService.sayHello(str);\n" +
                "        println(str);\n" +
                "        return \"HI \" + str;\n" +
                "    }\n" +
                "}\n";
        Class groovyClass  = groovyClassLoader.parseClass(str,"TestGroovyService.groovy");
        ITestService iTestService = (ITestService) groovyClass.newInstance();
        String strRes = iTestService.sayHello("aaa");
        System.out.println(strRes);
    }




}
